package com.adb.ww2.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.adb.ww2.R;

public class WarDialogFragment extends DialogFragment implements OnEditorActionListener{
	
	/**
	 * Create Interface that listen the dialog Done Interface
	 * 
	 */
	public interface EditNameDialogListener{
		void onFinishEditDialog(String inputText);
	}
	
	
	private EditText mEditText;
	  
	 /**
     * Create a new instance of WarDialogFragment
     */
	
	public static WarDialogFragment newInstance(){
		return new WarDialogFragment();
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onActivityCreated(Bundle arg0) {
		super.onActivityCreated(arg0);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View mView = inflater.inflate(R.layout.fragment_edit_name, container);
		mEditText = (EditText)mView.findViewById(R.id.txt_your_name);
		 // Show soft keyboard automatically
        mEditText.requestFocus();
        mEditText.setOnEditorActionListener(this);
		return mView;
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}
	@Override
	public void onDetach() {
		super.onDetach();
	}

	@Override
	public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
		
		if (EditorInfo.IME_ACTION_DONE == actionId) {
			//Return input string to Fragment Activity
			//Interface object 
			EditNameDialogListener mEditDialog = (EditNameDialogListener)getActivity();
			mEditDialog.onFinishEditDialog(mEditText.getText().toString());
			this.dismiss();
			return true;
		}
		return false;
	}
	
}
